package MazeGenProject;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;

import org.jhotdraw.figures.LineFigure;
import org.jhotdraw.framework.Drawing;
import org.jhotdraw.standard.GridConstrainer;

public class WallFigure extends LineFigure {
	
	private GridDrawing _gridDrawing;
	private Point _newStart;
	private Point _newEnd;
	
	public WallFigure(GridDrawing d){
		super();
		_gridDrawing = d;
	}
	
	public WallFigure(GridDrawing d, int x1, int y1, int x2, int y2)
	{
		super();
		startPoint(x1, y1);
		endPoint(x2, y2);
		_newStart = new Point(x1, y1);
		_newEnd = new Point(x2, y2);
		_gridDrawing = d;
	}
	
	public void snapToGrid(Point start, Point end){
		int space = _gridDrawing.getSpace();
		GridFigure[] columns = _gridDrawing.getGridColumns();
		GridFigure[] rows = _gridDrawing.getGridRows();
		int newSX = columns[(int) Math.floor(start.x/space)].startPoint().x;
		int newSY = rows[(int) Math.floor(start.y/space)].startPoint().y;
		int newEX = columns[(int) Math.floor(end.x/space)].startPoint().x;
		int newEY = rows[(int) Math.floor(end.y/space)].startPoint().y;
		_newStart = new Point(newSX,newSY);
		_newEnd = new Point (newEX, newEY);
		
		if(_newStart.x != _newEnd.x && _newStart.y != _newEnd.y){ // Make better
			_newEnd.y = _newEnd.y - _newStart.y;
			_newEnd.x = _newEnd.x = _newStart.x;
		}
		
	}
	
	
	
	@Override
	protected void drawLine(Graphics g, int x1, int y1, int x2, int y2) {
		g.setColor(Color.RED);
		snapToGrid(new Point(x1,y1), new Point(x2,y2));
		g.drawLine(_newStart.x, _newStart.y, _newEnd.x, _newEnd.y);
	}

}
